layui.use(['layer', 'form','upload', 'admin', 'ax','layedit'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var upload = layui.upload;
    var layedit = layui.layedit;
    layedit.set({
        uploadImage: {
            url: '/userName/images1' //接口url
        }
    });
    //实例化编辑器
    /*var ue = UE.getEditor('container', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,//滚动条
        initialFrameHeight: 400 //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/ueditor/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };*/

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/notice/detail?noticeId=" + Feng.getUrlParam("noticeId"));
    var result = ajax.start();
    form.val("noticeForm", result.data);
    $("#img1").attr("src",Feng.ctxPath+result.data.pic) ;
    $("#container").val(result.data.content) ;
    // 表单提交事件
    var index = layedit.build('container', {
        height: 180, //设置编辑器高度
        tool: ['left' //左对齐
            ,'center' //居中对齐
            ,'right','image']
    });


    $('#cancel').click(function () {
        window.location.href = Feng.ctxPath + "/notice";
    });
    upload.render({
        elem: '#picBtn'
        , url: Feng.ctxPath + '/userName/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#img1').attr('src', result);
            });
        }
        , done: function (res) {
            console.log(res);
            $("#pic").val(res.data);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传图片失败！");
        }
    });



    form.on('submit(btnSubmit)', function (data) {
        layedit.sync(index);
        var content = layedit.getContent(index);
        data.field.content=content;
        var ajax = new $ax(Feng.ctxPath + "/notice/update", function (data) {
            Feng.success("添加成功！");
            console.log($("#pic").val());
            window.location.href = Feng.ctxPath + "/notice";
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });

});